package org.pie.drawing.apps.picimageserver.service.impl;

import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.pie.drawing.apps.picimageserver.entity.DustProcessDto;
import org.pie.drawing.apps.picimageserver.entity.DustProcessEntity;
import org.pie.drawing.apps.picimageserver.entity.DustProcessTxt;
import org.pie.drawing.apps.picimageserver.mapper.DustProcessMapper;
import org.pie.drawing.apps.picimageserver.service.DustProcessService;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * @author zzj
 * @date 2024/09/16 21:01
 **/
@Service
@RequiredArgsConstructor
public class DustProcessServiceImpl extends ServiceImpl<DustProcessMapper, DustProcessEntity> implements DustProcessService {

    @Override
    public void txtSave(String path) {
        try {
            String jsonStr = new String(Files.readAllBytes(Paths.get(path)));
            JSONObject jsonObject = JSON.parseObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            List<DustProcessTxt> txts = JSON.parseArray(JSON.toJSONString(jsonArray), DustProcessTxt.class);
            for (DustProcessTxt dustProcessTxt : txts) {
                String dustArea = dustProcessTxt.getDustArea();
                if(StringUtils.isNotEmpty(dustArea)){
                    dustArea=dustArea.replaceAll(" ","");
                    dustArea=dustArea.replaceAll("万平方公里","");
                    dustProcessTxt.setDustArea(dustArea);
                }
                String dustStormArea = dustProcessTxt.getDustStormArea();
                if(StringUtils.isNotEmpty(dustStormArea)){
                    dustStormArea=dustStormArea.replaceAll(" ","");
                    dustStormArea=dustStormArea.replaceAll("万平方公里","");
                    dustProcessTxt.setDustStormArea(dustStormArea);
                }
                String strongDustStormArea = dustProcessTxt.getStrongDustStormArea();
                if(StringUtils.isNotEmpty(strongDustStormArea)){
                    strongDustStormArea=strongDustStormArea.replaceAll(" ","");
                    strongDustStormArea=strongDustStormArea.replaceAll("万平方公里","");
                    dustProcessTxt.setDustArea(strongDustStormArea);
                }
                DustProcessEntity dustProcess=JSON.parseObject(JSON.toJSONString(dustProcessTxt), DustProcessEntity.class);
                save(dustProcess);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<DustProcessEntity> dustList(DustProcessDto dto){
        LambdaQueryWrapper<DustProcessEntity> queryWrapper = new LambdaQueryWrapper<>();
        if("1".equals(dto.getTimeType())&& StringUtils.isNotEmpty(dto.getYear())){
            queryWrapper.eq(DustProcessEntity::getYear,dto.getYear());
        }
        if("2".equals(dto.getTimeType())){
            if(StringUtils.isNotEmpty(dto.getStartDate())){
                queryWrapper.ge(DustProcessEntity::getStartDate,dto.getStartDate());
            }
            if(StringUtils.isNotEmpty(dto.getEndDate())){
                queryWrapper.le(DustProcessEntity::getEndDate,dto.getEndDate());
            }
        }
        if(StringUtils.isNotEmpty(dto.getProcessType())){
            queryWrapper.eq(DustProcessEntity::getProcessType,dto.getProcessType());
        }
        if(StringUtils.isNotEmpty(dto.getSortType())){
            if("time".equals(dto.getSortType())){
                if("1".equals(dto.getSortMethod())){
                    queryWrapper.orderByAsc(DustProcessEntity::getStartDate);
                }else {
                    queryWrapper.orderByDesc(DustProcessEntity::getStartDate);
                }
            }
            if("dustArea".equals(dto.getSortType())){
                if("1".equals(dto.getSortMethod())){
                    queryWrapper.orderByAsc(DustProcessEntity::getDustArea);
                }else {
                    queryWrapper.orderByDesc(DustProcessEntity::getDustArea);
                }
            }
            if("dustStormArea".equals(dto.getSortType())){
                if("1".equals(dto.getSortMethod())){
                    queryWrapper.orderByAsc(DustProcessEntity::getDustStormArea);
                }else {
                    queryWrapper.orderByDesc(DustProcessEntity::getDustStormArea);
                }
            }
            if("strongDustStormArea".equals(dto.getSortType())){
                if("1".equals(dto.getSortMethod())){
                    queryWrapper.orderByAsc(DustProcessEntity::getStrongDustStormArea);
                }else {
                    queryWrapper.orderByDesc(DustProcessEntity::getStrongDustStormArea);
                }
            }
            queryWrapper.like(DustProcessEntity::getDescribeT,"新疆");
            queryWrapper.or().like(DustProcessEntity::getDustMaxPM10Stations,"新疆")
                    .or().like(DustProcessEntity::getDustMinVisStations,"新疆")
                    .or().like(DustProcessEntity::getDustMaxWindStations,"新疆");
        }
        List<DustProcessEntity> list = baseMapper.selectList(queryWrapper);
        return list;

    }

    @Override
    public void export(DustProcessDto dto, HttpServletResponse response){
        try {
            List<DustProcessEntity> list=this.dustList(dto);
            String fileName = "test.xlsx";
//            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
            response.addHeader("Access-Control-Expose-Headers", "content-disposition");
            response.addHeader("Content-Disposition", "attachment;filename=" +fileName );
            response.setContentType("application/octet-stream");
            ServletOutputStream outputStream = response.getOutputStream();
            EasyExcelFactory.write(outputStream,DustProcessEntity.class).sheet().doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
